﻿<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="Shortcut Icon" href="../images/aecc.ico" type="image/x-icon" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="../StaticFiles/element-ui/element-ui.css">
  <link rel="stylesheet" href="../StaticFiles/nprogress/nprogress.min.css">
  <link rel="stylesheet" href="../styles/vue-page-common.css">
  <script src="../Scripts/vue.min.js"></script>
  <script src="../StaticFiles/element-ui/element-ui.js"></script>
  <script src="../Scripts/axios.min.js"></script>
  <script src="../StaticFiles/nprogress/nprogress.min.js"></script>
  <script src="../Scripts/dayjs.min.js"></script>
  <script src="../Scripts/axios.config.js"></script>
  <title>工时查询报表</title>
  <style>
    #queryZone .red {
      color: red;
    }

    .lable {
      padding: 0 3px;
    }

    .red {
      color: red;
    }

    .blue {
      color: blue;
    }
  </style>
</head>

<body>
  <div id="app" v-cloak v-on:keyup.enter="getData()">
    <!-- 报表标题 -->
    <vue2-report-header :title="'工时查询报表'"></vue2-report-header>
    <div style="padding: 10px;" id="queryZone">
      <!-- 查询条件区 -->
      <el-form :inline="true" :model="queryInfo" ref="queryInfoRef" :label-position="'left'" :rules="rules"
        :disabled="formDisabled">
        <el-form-item label="主制分厂:" prop="factoryid">
          <el-select v-model="queryInfo.factoryid" placeholder="请选择主制分厂" filterable @change="factorySelectChange"
            :disabled="factoryDisabled">
            <el-option v-for="f in factoryList" :key="f.factoryid" :label="f.factoryname" :value="f.factoryid">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="主制工区:">
          <el-select v-model="queryInfo.workcenterid" placeholder="请选择主制工区" clearable filterable
            @change="workcenterSelectChange" :disabled="workcenterDisabled">
            <el-option v-for="w in workcenterList" :key="w.workcenterid" :label="w.workcentername"
              :value="w.workcenterid">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="班组:">
          <el-select v-model="queryInfo.teamid" placeholder="请选择班组" clearable filterable @change="teamSelectChange"
            :disabled="teamDisabled">
            <el-option v-for="w in teamList" :key="w.teamid" :label="w.teamname" :value="w.teamid">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="工人:">
          <el-select v-model="queryInfo.workerid" placeholder="请选择" clearable filterable :disabled="workerDisabled"
            style="width: 120px;">
            <el-option v-for="i in workerList" :key="i.employeeid" :label="i.fullname" :value="i.employeeid">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="机型:">
          <el-select v-model="queryInfo.productfamilyname" placeholder="请选择机型" clearable filterable>
            <el-option v-for="p in productfamilyList" :key="p.productfamilyname" :label="p.productfamilyname"
              :value="p.productfamilyname">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item prop='productname' label="零件图号:">
          <el-autocomplete v-model.trim="queryInfo.productname" placeholder="请输入零件图号" :trigger-on-focus="false"
            style="width:240px;" :fetch-suggestions="searchProductAsync" :value-key="'productname'" :debounce="500">
          </el-autocomplete>
        </el-form-item>

        <el-form-item prop='containername' label="流水卡号:">
          <el-autocomplete v-model.trim="queryInfo.containername" placeholder="请输入流水卡号" :trigger-on-focus="false"
            style="width:240px;" :fetch-suggestions="searchContainerAsync" :value-key="'containername'" :debounce="500">
          </el-autocomplete>
        </el-form-item>

        <el-form-item label="提交日期:">
          <el-date-picker v-model="queryInfo.tijiaodate" type="daterange" range-separator="至" value-format="yyyy-MM-dd"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
        </el-form-item>

        <el-form-item label="检验日期:" prop="checkdate">
          <el-date-picker v-model="queryInfo.checkdate" type="daterange" range-separator="至" value-format="yyyy-MM-dd"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
        </el-form-item>

        <el-form-item label="状态:">
          <el-select v-model="queryInfo.status" placeholder="请选择" clearable filterable style="width: 120px;">
            <el-option :key="'未提交'" :label="'未提交'" :value="'未提交'"></el-option>
            <el-option :key="'已提交'" :label="'已提交'" :value="'已提交'"></el-option>
            <el-option :key="'已核算'" :label="'已核算'" :value="'已核算'"></el-option>
            <el-option :key="'已核销'" :label="'已核销'" :value="'已核销'"></el-option>
          </el-select>
        </el-form-item>

        <div>
          <el-form-item>
            <template slot="label">
              <el-popover placement="top-start" title="" width="300" trigger="hover" content="\n ">
                <i class="el-icon-info" slot="reference"></i>
                <p>汇总工时 = 下方表格中所有工人的个人工时之和</p>
              </el-popover>
            </template>
            <el-form-item label="汇总工时:">
              <el-tag type="danger">{{totalworktime}} 小时</el-tag>
            </el-form-item>
          </el-form-item>

          <el-form-item>
            <el-popover placement="top-start" title="个人工时的说明" width="600" trigger="hover" content="">
              <el-tag class="el-icon-info" slot="reference"> 个人工时</el-tag>
              <p>
                1.【个人工时】<strong class="red lable">会</strong>随报表筛选项<strong class="blue lable">检验日期</strong>的改变而改变。
              </p>
            </el-popover>
          </el-form-item>

          <el-form-item>
            <el-popover placement="top-start" title="本月工时的说明如下" width="600" trigger="hover" content="">
              <el-tag class="el-icon-info" slot="reference"> 本月工时</el-tag>
              <p>
                1.【工时统计节点】：【盘轴制造分厂】为<strong class="red lable">每月25日</strong>，【工装工具分公司】为<strong
                  class="red lable">每月26日</strong>，其他分厂为<strong class="blue lable">每月19日</strong>。
              </p>
              <p>
                2.【本月工时】：计算从【工时统计节点】到【系统当前时间】所产生的工时。<strong class="red lable">月初</strong>统计的是上月19日
                ~【系统当前时间】工时,<strong class="blue lable">月末</strong>统计的是本月19日~【系统当前时间】工时。
                <br />
                <strong class="lable">例如:</strong>若系统当前时间为12月10日，本月工时计算：11月19日 ~ 12月10日工时。若系统当前时间是12月21日，本月工时计算：12月19日 ~
                12月21日工时。
              </p>
              <p>
                3.【结构件制造分厂】不显示【本月工时】。
              </p>
              <p>
                4.【本月工时】<strong class="red lable">不会</strong>随报表筛选项<strong class="blue lable">检验日期</strong>的改变而改变。
              </p>
            </el-popover>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="small" @click="getData">查询</el-button>
            <el-button type="primary" icon="el-icon-download" size="small" @click="getExportExcel">导出</el-button>
          </el-form-item>
        </div>
      </el-form>

      <!-- 表格显示区 -->
      <el-table v-loading="loading" v-loading.fullscreen.lock="loading" :element-loading-text="loadingText"
        element-loading-background="rgba(0,0,0,0.8)" :data="dataList" stripe border>
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column label="操作" width="125">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="getWorktimeDetail(scope.row)">
              工时明细
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="员工id" prop="employeeid" v-if="false"></el-table-column>
        <el-table-column label="姓名" prop="fullname"></el-table-column>
        <el-table-column label="员工号" prop="employeename"></el-table-column>
        <el-table-column label="个人工时(小时)" prop="worktime" sortable></el-table-column>
        <!-- 结构件分厂不显示本月工时列 -->
        <el-table-column label="本月工时(小时)" prop="thismonthworktime" v-if="queryInfo.factorycode !== '609'">
        </el-table-column>
      </el-table>

      <!-- 员工工时--明细对话框 -->
      <el-dialog :title="workTimeDetail.title" :width="'98%'" :top="'10px'"
        :visible.sync="workTimeDetail.dialogVisiable">
        <el-table v-loading="loading" v-loading.fullscreen.lock="loading" :element-loading-text="loadingText"
          element-loading-background="rgba(0,0,0,0.8)" :data="workTimeDetail.dataList" stripe border>
          <el-table-column type="index" width="40"></el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-download" size="mini" @click="exportWorktimeDetail">
                导出
              </el-button>
            </template>
          </el-table-column>
          <el-table-column label="订单号" prop="mfgordername" width="120" show-overflow-tooltip></el-table-column>
          <el-table-column label="机型" prop="familylistnames" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="流水卡号" prop="containername" show-overflow-tooltip></el-table-column>
          <el-table-column label="工序" prop="workflowstepname" width="140" show-overflow-tooltip></el-table-column>
          <el-table-column label="工区" prop="workcentername" width="120" show-overflow-tooltip></el-table-column>
          <el-table-column label="报工时间" prop="reporttime" width="100" :formatter="splitDate"></el-table-column>
          <el-table-column label="检验时间" prop="checkdate" width="100" :formatter="splitDate"></el-table-column>
          <el-table-column label="单位工时(分)" prop="durationperunit" width="110" show-overflow-tooltip></el-table-column>
          <el-table-column label="合格数" prop="qty" width="60" show-overflow-tooltip></el-table-column>
          <el-table-column label="合计工时(分)" prop="sumworktime" width="110" show-overflow-tooltip></el-table-column>
          <el-table-column label="是否提交" prop="istijiao" width="80"></el-table-column>
          <el-table-column label="是否核销" prop="ishexiao" width="80"></el-table-column>
          <el-table-column label="是否核算" prop="ishesuan" width="80"></el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination @current-change="handleWorkTimeDetailCurrentChange"
          :current-page="workTimeDetail.queryInfo.currentPage" :page-sizes="[12]"
          :page-size="workTimeDetail.queryInfo.pageSize" layout="prev,pager,next,jumper,sizes,total"
          :total="workTimeDetail.total">
        </el-pagination>
      </el-dialog>

      <!-- 回到顶部 -->
      <el-backtop></el-backtop>
    </div>
  </div>

  <script type="module">
    import { getParams, fetchSuggestion, exportExcelData } from '../Scripts/hooks/useCommonMethod.js'
    import { getProductFamily, getUserInfo, getFactoryListByCode, getWorkcenterList, getTeamList, getWorkerList } from '../Scripts/hooks/fetchAPI.js'
    import { prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth } from '../Scripts/hooks/useDateShortCut.js'
    import { splitDate, replaceDate } from '../Scripts/hooks/format.js'
    import Vue2ReportHeader from '../Scripts/components/Vue2ReportHeader.js'
    const app = new Vue({
      el: '#app',
      components: { Vue2ReportHeader },
      data: {
        splitDate, replaceDate,
        total: 0,
        loading: false,
        loadingText: '数据加载中...',
        queryInfo: {
          workerid: '',
          factoryid: '',//分厂
          factorycode: '',//分厂代码
          workcenterid: '',// 工区
          teamid: '',// 班组
          tijiaodate: '',//提交时间
          checkdate: '',// 检验时间
          thismonth: '',// 本月工时筛选时间范围
          productfamilyname: '',
          productname: '',
          containername: '',
          status: '未提交',
          currentPage: 1,
          pageSize: 12
        },
        employeeid: '',
        dataList: [],
        factoryList: [],
        workcenterList: [],
        teamList: [],
        workerList: [],
        productfamilyList: [],
        userInfo: {},
        factoryDisabled: false,
        workcenterDisabled: false,
        teamDisabled: false,
        workerDisabled: false,
        formDisabled: false,
        totalworktime: '',//汇总工时
        workTimeDetail: {
          dialogVisiable: false,
          queryInfo: {
            employeeid: '',
            fullname: '',
            currentPage: 1,//当前页
            pageSize: 12//每页显示条数
          },
          dataList: [],
          total: 0,
          title: ''
        },
        rules: {
          factoryid: [{ required: true, message: '主制分厂不能为空', trigger: 'change' }],
          checkdate: [{ required: true, message: '检验日期不能为空', trigger: 'change' }]
        },
        pickerOptions: { shortcuts: [prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth] }
      },
      methods: {
        // 查询件号
        searchProductAsync(value, callback) {
          fetchSuggestion({ actionName: 'getProductnameAsync', name: 'productname', value, callback });
        },
        // 查询批次号
        searchContainerAsync(value, callback) {
          fetchSuggestion({ actionName: 'getContainernameAsync', name: 'containername', value, callback });
        },
        async factorySelectChange(val) {
          this.queryInfo.workcenterid = null;
          this.queryInfo.teamid = null;
          this.queryInfo.workerid = null;
          if (val) {
            this.queryInfo.factorycode = this.factoryList.filter(item => val === item.factoryid)[0].attributevalue;
            this.workcenterList = await getWorkcenterList({ factoryid: val });
            this.workerList = await getWorkerList({ ...this.queryInfo });
            this.setStatisticsDate();
          }
        },
        async workcenterSelectChange(val) {
          this.queryInfo.teamid = null;
          this.queryInfo.workerid = null;
          if (val) {
            this.teamList = await getTeamList({ workcenterid: val });
            this.workerList = await getWorkerList({ ...this.queryInfo });
          }
        },
        async teamSelectChange(val) {
          this.queryInfo.workerid = null;
          if (val) {
            this.workerList = await getWorkerList({ ...this.queryInfo });
          }
        },
        //查询数据
        async getData() {
          this.$refs['queryInfoRef'].validate(async (valid) => {
            if (!valid) {
              return this.$message.error('必填项不能为空');
            } else {
              this.totalworktime = '';
              this.loading = true;
              this.loadingText = '数据加载中...';
              const { data: { data, meta, total } } = await axios.post('../handler/report/EmployeeWorkTimeCountReport.ashx?action=getData', {
                type: 'getData',
                ...this.queryInfo
              })
              this.loading = false;
              if (meta.status === 500) { return this.$message.error(meta.msg); }
              this.dataList = data;
              if (data.length > 0) {
                // 求和总工时
                this.totalworktime = data.reduceRight((sum, current) => sum + current.worktime, 0).toFixed(2);
              }
            }
          })
        },
        // 查询工时明细
        async getWorktimeDetail(row) {
          this.workTimeDetail.dialogVisiable = true;
          this.workTimeDetail.queryInfo.currentPage = 1;
          this.workTimeDetail.queryInfo.employeeid = row.employeeid;
          this.workTimeDetail.queryInfo.fullname = row.fullname;
          this.getWorkerWorkTimeDetailData();
        },
        async getWorkerWorkTimeDetailData() {
          this.workTimeDetail.title = "【" + this.workTimeDetail.queryInfo.fullname + "】工时明细";
          this.loading = true;
          this.loadingText = '数据加载中...';
          const { data: { data, meta, total } } = await axios.post('../handler/report/EmployeeWorkTimeCountReport.ashx?action=getWorktimeDetail', {
            type: 'getWorktimeDetail',
            ...this.queryInfo,
            ...this.workTimeDetail.queryInfo
          })
          this.loading = false;
          if (meta.status === 500) { return this.$message.error(meta.msg); }
          this.workTimeDetail.dataList = data;
          this.workTimeDetail.total = total;
        },
        handleWorkTimeDetailCurrentChange(newPage) {
          this.workTimeDetail.queryInfo.currentPage = newPage;
          this.getWorkerWorkTimeDetailData();
        },
        // 工时明显导出
        exportWorktimeDetail() {
          const config = {
            url: '../handler/report/EmployeeWorkTimeCountReport.ashx?action=exportWorktimeDetail',
            data: { type: 'exportWorktimeDetail', ...this.queryInfo, ...this.workTimeDetail.queryInfo }
          };
          exportExcelData(config, this);
        },
        // 导出
        getExportExcel() {
          const config = {
            url: '../handler/report/EmployeeWorkTimeCountReport.ashx?action=getExportExcel',
            data: { type: 'getExportExcel', ...this.queryInfo }
          };
          exportExcelData(config, this);
        },
        // 设置工时统计节点
        setStatisticsDate() {
          const { factorycode } = this.queryInfo;
          let startOfCheckdate = '';
          const today = dayjs().format('YYYY-MM-DD');
          // 工时统计节点
          let statisticsDate;
          if (factorycode === '600') {
            statisticsDate = 25;// 盘轴制造分厂【工时统计节点】为每月25日
          } else if (factorycode === '604') {
            statisticsDate = 26;// 工装工具分公司【工时统计节点】为每月26日
          } else {
            statisticsDate = 19;// 其他分厂【工时统计节点】为每月19日
          }

          if (dayjs().get('date') <= statisticsDate) {
            startOfCheckdate = dayjs().startOf('month').subtract(1, 'month').add(statisticsDate - 1, 'day').format('YYYY-MM-DD');
          } else {
            startOfCheckdate = dayjs().startOf('month').add(statisticsDate - 1, 'day').format('YYYY-MM-DD');
          }

          this.queryInfo.checkdate = [startOfCheckdate, today];
          this.queryInfo.thismonth = [startOfCheckdate, today];
        }
      },
      async mounted() {
        const uid = getParams("uid");
        if (!uid) {
          this.formDisabled = true;
          return this.$notify.error('缺少用户参数');
        }

        this.userInfo = await getUserInfo({ employeeid: uid });
        if (!this.userInfo) {
          this.formDisabled = true;
          return this.$notify.error('用户不存在');
        }
        // 解构获取用户信息
        const { factoryid, factoryname, factorycode, workcenterid, teamid, employeeid, auth } = this.userInfo;
        if (auth === '工人') {
          this.queryInfo.factoryid = factoryid;
          this.queryInfo.workcenterid = workcenterid;
          this.queryInfo.teamid = teamid;
          this.queryInfo.workerid = employeeid;
          this.queryInfo.factorycode = factorycode;
          this.workcenterList.push({ ...this.userInfo });
          this.teamList.push({ ...this.userInfo });
          this.workerList.push({ ...this.userInfo });
          this.factoryDisabled = true;
          this.workcenterDisabled = true;
          this.teamDisabled = true;
          this.workerDisabled = true;
        }
        if (auth === '班组长') {
          this.queryInfo.factoryid = factoryid;
          this.queryInfo.workcenterid = workcenterid;
          this.queryInfo.teamid = teamid;
          this.queryInfo.factorycode = factorycode;
          this.workcenterList.push({ ...this.userInfo });
          this.teamList.push({ ...this.userInfo });
          this.factoryDisabled = true;
          this.workcenterDisabled = true;
          this.teamDisabled = true;
          this.workerList = await getWorkerList({ ...this.queryInfo });
        }
        if (auth === '工区长') {
          this.queryInfo.factoryid = factoryid;
          this.queryInfo.workcenterid = workcenterid;
          this.queryInfo.factorycode = factorycode;
          this.workcenterList.push({ ...this.userInfo });
          this.factoryDisabled = true;
          this.workcenterDisabled = true;
          this.teamList = await getTeamList({ workcenterid });
          this.workerList = await getWorkerList({ ...this.queryInfo });
        }
        if (auth === '管理员' && factoryid !== null) {
          this.queryInfo.factoryid = factoryid;
          this.queryInfo.factorycode = factorycode;
          this.factoryDisabled = true;
          this.workcenterList = await getWorkcenterList({ factoryid });
          this.workerList = await getWorkerList({ ...this.queryInfo });
        }
        if (auth === null && factoryname !== '生产指挥部') {
          this.formDisabled = true;
          this.$message.error('该用户无权使用该报表');
        }
        this.setStatisticsDate();
        // 机匣、结构件、盘轴、燃烧、叶片、工装、热表
        this.factoryList = await getFactoryListByCode({ factoryCode: '600|601|602|604|607|608|609' });
        this.productfamilyList = await getProductFamily();
      }
    })
  </script>
</body>

</html>